﻿using System;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;

using Com.Ompchina.Lib.DBHelper;
using IDAL.Config;
using Model.Config;
namespace SQLServerDAL.Config
{
    /// <summary>
    /// 数据层，内容管理，术语
    /// </summary>
    public class TermDAL : DalBase, ITerm
    {
        public TermDAL() 
        {
            Schema = "cms.";
            TableName = "Term";
            ViewTableName = "V_Term";
            Fields = " TermID, TermLabel, TermName, TermDescription, HasSub, AddTime, ParentTermID, ParentTermLabel, ParentTermName ";
        }
        #region ITerm 成员
        /// <summary>
        /// 新增术语
        /// </summary>
        /// <param name="info"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public int InsertTerm(TermInfo info, out string message)
        {
            SqlParameter[] parm = new SqlParameter[] { 
                new SqlParameter("@TermLabel",SqlDbType.VarChar,10)
                ,new SqlParameter("@TermName",SqlDbType.NVarChar,20)
                ,new SqlParameter("@TermDescription",SqlDbType.NVarChar,100)
                ,new SqlParameter("@ParentTerm",SqlDbType.Int)
                ,new SqlParameter("@RtnID",SqlDbType.Int)
                ,new SqlParameter("@RtnCode",SqlDbType.Char,2)
                ,new SqlParameter("@RtnMsg",SqlDbType.NVarChar,100)
            };
            int i = 0;
            parm[i++].Value = info.TermLabel;
            parm[i++].Value = info.TermName;
            parm[i++].Value = info.TermDescription;
            parm[i++].Value = info.ParentTerm.TermID;
            parm[i++].Direction = ParameterDirection.Output;
            parm[i++].Direction = ParameterDirection.Output;
            parm[i++].Direction = ParameterDirection.Output;
            SqlHelper.ExecuteNonQuery(base.ConnectionString, CommandType.StoredProcedure, Schema + "AddTerm", parm);
            message = parm[parm.Length - 1].Value.ToString();
            return parm[parm.Length - 2].Value.ToString().Equals("80") ?
                int.Parse(parm[parm.Length - 3].Value.ToString()) : 0;
        }

        /// <summary>
        /// 修改术语
        /// </summary>
        /// <param name="info"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public bool UpdateTerm(TermInfo info, out string message)
        {
            SqlParameter[] parm = new SqlParameter[] { 
                new SqlParameter("@TermID",SqlDbType.Int)
                ,new SqlParameter("@TermLabel",SqlDbType.VarChar,10)
                ,new SqlParameter("@TermName",SqlDbType.NVarChar,20)
                ,new SqlParameter("@TermDescription",SqlDbType.NVarChar,100)
                ,new SqlParameter("@ParentTerm",SqlDbType.Int)
                ,new SqlParameter("@RtnCode",SqlDbType.Char,2)
                ,new SqlParameter("@RtnMsg",SqlDbType.NVarChar,100)
            };
            int i = 0;
            parm[i++].Value = info.TermID;
            parm[i++].Value = info.TermLabel;
            parm[i++].Value = info.TermName;
            parm[i++].Value = info.TermDescription;
            parm[i++].Value = info.ParentTerm.TermID;
            parm[i++].Direction = ParameterDirection.Output;
            parm[i++].Direction = ParameterDirection.Output;
            SqlHelper.ExecuteNonQuery(base.ConnectionString, CommandType.StoredProcedure, Schema + "UpdateTerm", parm);
            message = parm[parm.Length - 1].Value.ToString();
            return parm[parm.Length - 2].Value.ToString().Equals("80");
        }

        /// <summary>
        /// 根据编号获取术语
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public TermInfo GetTermByID(int id)
        {
            Sql = string.Format("SELECT TOP 1 {0} FROM {1} WHERE TermID = {2}", Fields, Schema + ViewTableName, id);
            SqlDataReader dr = SqlHelper.ExecuteReader(base.ConnectionString, CommandType.Text, Sql);
            TermInfo info = new TermInfo();
            if (dr != null) 
            {
                if (dr.Read()) 
                {
                    int i = 0;
                    info.TermID = dr.GetInt32(i++);
                    info.TermLabel = dr.GetString(i++);
                    info.TermName = dr.GetString(i++);
                    info.TermDescription = dr.GetString(i++);
                    info.HasSub = dr.GetString(i++);
                    info.AddTime = dr.GetDateTime(i++);
                    info.ParentTerm = new TermInfo();
                    info.ParentTerm.TermID = dr.GetInt32(i++);
                    info.ParentTerm.TermLabel = dr.GetString(i++);
                    info.ParentTerm.TermName = dr.GetString(i++);
                }
                dr.Close();
            }
            return info;
        }

        /// <summary>
        /// 术语列表
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        public IList<TermInfo> ListTerm(TermCondition condition)
        {
            SqlBuilder = new StringBuilder("SELECT ");
            if (condition.Top > 0)
                SqlBuilder.AppendFormat(" TOP({0}) ", condition.Top);
            SqlBuilder.AppendFormat(" {0} FROM {1} WHERE 1 = 1 ", Fields, Schema + ViewTableName);
            if (condition.ParentTermID >= 0)
                SqlBuilder.AppendFormat(" AND ParentTermID = {0}", condition.ParentTermID);
            if (!string.IsNullOrEmpty(condition.ParentTermLabel))
                SqlBuilder.AppendFormat(" AND ParentTermLabel = '{0}'", condition.ParentTermLabel);
            if (!string.IsNullOrEmpty(condition.OrderBy))
                SqlBuilder.AppendFormat(" ORDER BY {0} ", condition.OrderBy);
            SqlDataReader dr = SqlHelper.ExecuteReader(base.ConnectionString, CommandType.Text, SqlBuilder.ToString());
            List<TermInfo> list = new List<TermInfo>();
            if (dr != null)
            {
                while (dr.Read())
                {
                    TermInfo info = new TermInfo();
                    int i = 0;
                    info.TermID = dr.GetInt32(i++);
                    info.TermLabel = dr.GetString(i++);
                    info.TermName = dr.GetString(i++);
                    info.TermDescription = dr.GetString(i++);
                    info.HasSub = dr.GetString(i++);
                    info.AddTime = dr.GetDateTime(i++);
                    info.ParentTerm = new TermInfo();
                    info.ParentTerm.TermID = dr.GetInt32(i++);
                    info.ParentTerm.TermLabel = dr.GetString(i++);
                    info.ParentTerm.TermName = dr.GetString(i++);
                    list.Add(info);
                }
                dr.Close();
            }
            return list;
        }

        /// <summary>
        /// 属于列表，分页
        /// </summary>
        /// <param name="condition"></param>
        /// <param name="recordCount"></param>
        /// <returns></returns>
        public IList<TermInfo> ListTerm(TermCondition condition, out int recordCount)
        {
            Wheres = new StringBuilder(" 1 = 1 ");
            if (condition.ParentTermID >= 0)
                Wheres.AppendFormat(" AND ParentTermID = {0}", condition.ParentTermID);
            if (!string.IsNullOrEmpty(condition.ParentTermLabel))
                Wheres.AppendFormat(" AND ParentTermLabel = '{0}'", condition.ParentTermLabel);
            recordCount = base.PageListCounter(Schema + ViewTableName, Wheres.ToString());
            SqlDataReader dr = base.PageListRecord(Fields, Schema + ViewTableName, Wheres.ToString()
                , condition.OrderBy, condition.PageIndex, condition.PageSize);
            List<TermInfo> list = new List<TermInfo>();
            if (dr != null)
            {
                while (dr.Read())
                {
                    TermInfo info = new TermInfo();
                    int i = 0;
                    info.TermID = dr.GetInt32(i++);
                    info.TermLabel = dr.GetString(i++);
                    info.TermName = dr.GetString(i++);
                    info.TermDescription = dr.GetString(i++);
                    info.HasSub = dr.GetString(i++);
                    info.AddTime = dr.GetDateTime(i++);
                    info.ParentTerm = new TermInfo();
                    info.ParentTerm.TermID = dr.GetInt32(i++);
                    info.ParentTerm.TermLabel = dr.GetString(i++);
                    info.ParentTerm.TermName = dr.GetString(i++);
                    list.Add(info);
                }
                dr.Close();
            }
            return list;
        }
        #endregion
    }
}
